package com.hzhr.iot.cqgas.ds.service;

import com.hzhr.iot.core.constant.CommonOptions;
import com.hzhr.iot.core.util.HzhrTypeUtil;
import com.hzhr.iot.cqgas.ds.dao.device.example.DevCollectDeviceTypeDao;
import com.hzhr.iot.cqgas.ds.dao.device.example.DevProdFactoryDao;
import com.hzhr.iot.cqgas.ds.dao.smartmeter.custom.EneMeterTypeCustomDao;
import com.hzhr.iot.cqgas.ds.dao.smartmeter.example.EneMeterTypeDao;
import com.hzhr.iot.cqgas.ds.dao.smartmeter.example.SmConcentratorTypeDao;
import com.hzhr.iot.cqgas.ds.dao.smartmeter.example.SmDtuTypeDao;
import com.hzhr.iot.cqgas.ds.entity.device.DevCollectDeviceTypeEntity;
import com.hzhr.iot.cqgas.ds.entity.device.DevCollectDeviceTypeExample;
import com.hzhr.iot.cqgas.ds.entity.device.DevProdFactoryEntity;
import com.hzhr.iot.cqgas.ds.entity.device.DevProdFactoryExample;
import com.hzhr.iot.cqgas.ds.entity.smartmeter.EneMeterTypeEntity;
import com.hzhr.iot.cqgas.ds.entity.smartmeter.EneMeterTypeExample;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class CollectDeviceTypeService {
    @Autowired
    DevCollectDeviceTypeDao devCollectDeviceTypeDao;
    @Autowired
    EneMeterTypeDao eneMeterTypeDao;
    @Autowired
    DevOptionsService devOptionsService;
    @Autowired
    SmDtuTypeDao smDtuTypeDao;
    @Autowired
    SmConcentratorTypeDao smConcentratorTypeDao;
    @Autowired
    EneMeterTypeCustomDao eneMeterTypeCustomDao;
    @Autowired
    DevProdFactoryDao devProdFactoryDao;

    @Value("${companyId}")
    String companyId;

    private Logger logger = LoggerFactory.getLogger(CollectDeviceTypeService.class);

    public void syncCollectDeviceType() {
        int addCount = 0;
        int updateCount = 0;
        long start = new Date().getTime();

        //同步gprs物联网表型号
        String iotCategoryCode = devOptionsService.getCode("COLLECT_DEVICE_CATEGORY", "GPRS_IOT_GAS_METER");
        EneMeterTypeExample eneMeterTypeExample = new EneMeterTypeExample();
        eneMeterTypeExample.createCriteria()
                .andRemoteMeterIdEqualTo("130")
                .andMeterCatagoryIdEqualTo("13");
        List<EneMeterTypeEntity> eneMeterTypeEntities = eneMeterTypeDao.selectByExample(eneMeterTypeExample);

        for (EneMeterTypeEntity eneMeterTypeEntity :
        eneMeterTypeEntities) {
            try {
                String mtId = eneMeterTypeEntity.getMtId();

                DevCollectDeviceTypeExample devCollectDeviceTypeExample = new DevCollectDeviceTypeExample();
                devCollectDeviceTypeExample.createCriteria()
                        .andOutIdEqualTo(mtId)
                        .andDeviceCategoryCodeEqualTo(iotCategoryCode);
                List<DevCollectDeviceTypeEntity> devCollectDeviceTypeEntities = devCollectDeviceTypeDao.selectByExample(devCollectDeviceTypeExample);
                if (devCollectDeviceTypeEntities.size() > 0) {
                    DevCollectDeviceTypeEntity devCollectDeviceTypeEntity = devCollectDeviceTypeEntities.get(0);
                    devCollectDeviceTypeEntity.setDes(HzhrTypeUtil.object2string(eneMeterTypeEntity.getMtModel()));
                    devCollectDeviceTypeEntity.setUpdateTime(new Date());
                    devCollectDeviceTypeEntity.setUpdateUserId(0);
                    devCollectDeviceTypeEntity.setIsValid(CommonOptions.YES_NO.YES);
                    devCollectDeviceTypeEntity.setState("1");

                    DevProdFactoryExample prodFactoryExample = new DevProdFactoryExample();
                    prodFactoryExample.createCriteria().andOutIdEqualTo(eneMeterTypeEntity.getPrId());
                    List<DevProdFactoryEntity> devProdFactoryEntities = devProdFactoryDao.selectByExample(prodFactoryExample);
                    if (devProdFactoryEntities.size() == 0) {
                        devCollectDeviceTypeEntity.setProdFactoryId(0);
                    } else {
                        DevProdFactoryEntity prodFactoryEntity = devProdFactoryEntities.get(0);
                        devCollectDeviceTypeEntity.setProdFactoryId(prodFactoryEntity.getId());

                    }

                    devCollectDeviceTypeDao.updateByPrimaryKey(devCollectDeviceTypeEntity);

                    updateCount++;
                } else {
                    DevCollectDeviceTypeEntity devCollectDeviceTypeEntity = new DevCollectDeviceTypeEntity();
                    devCollectDeviceTypeEntity.setOutId(eneMeterTypeEntity.getMtId());
                    devCollectDeviceTypeEntity.setDes(HzhrTypeUtil.object2string(eneMeterTypeEntity.getMtModel()));
                    devCollectDeviceTypeEntity.setCompanyId(Integer.parseInt(companyId));
                    devCollectDeviceTypeEntity.setCreateTime(new Date());
                    devCollectDeviceTypeEntity.setCreateUserId(0);
                    devCollectDeviceTypeEntity.setIsValid(CommonOptions.YES_NO.YES);
                    devCollectDeviceTypeEntity.setState("1");
                    devCollectDeviceTypeEntity.setDeviceCategoryCode(iotCategoryCode);

                    DevProdFactoryExample prodFactoryExample = new DevProdFactoryExample();
                    prodFactoryExample.createCriteria().andOutIdEqualTo(eneMeterTypeEntity.getPrId());
                    List<DevProdFactoryEntity> devProdFactoryEntities = devProdFactoryDao.selectByExample(prodFactoryExample);
                    if (devProdFactoryEntities.size() == 0) {
                        devCollectDeviceTypeEntity.setProdFactoryId(0);
                    } else {
                        DevProdFactoryEntity prodFactoryEntity = devProdFactoryEntities.get(0);
                        devCollectDeviceTypeEntity.setProdFactoryId(prodFactoryEntity.getId());

                    }

                    devCollectDeviceTypeDao.insert(devCollectDeviceTypeEntity);
                    addCount++;
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }

        //同步nb物联网表型号
        iotCategoryCode = devOptionsService.getCode("COLLECT_DEVICE_CATEGORY", "NB_IOT_GAS_METER");
        eneMeterTypeExample = new EneMeterTypeExample();
        eneMeterTypeExample.createCriteria()
                .andRemoteMeterIdEqualTo("138")
                .andMeterCatagoryIdEqualTo("13");
        eneMeterTypeEntities = eneMeterTypeDao.selectByExample(eneMeterTypeExample);

        for (EneMeterTypeEntity eneMeterTypeEntity :
                eneMeterTypeEntities) {
            try {
                String mtId = eneMeterTypeEntity.getMtId();

                DevCollectDeviceTypeExample devCollectDeviceTypeExample = new DevCollectDeviceTypeExample();
                devCollectDeviceTypeExample.createCriteria()
                        .andOutIdEqualTo(mtId)
                        .andDeviceCategoryCodeEqualTo(iotCategoryCode);
                List<DevCollectDeviceTypeEntity> devCollectDeviceTypeEntities = devCollectDeviceTypeDao.selectByExample(devCollectDeviceTypeExample);
                if (devCollectDeviceTypeEntities.size() > 0) {
                    DevCollectDeviceTypeEntity devCollectDeviceTypeEntity = devCollectDeviceTypeEntities.get(0);
                    devCollectDeviceTypeEntity.setDes(HzhrTypeUtil.object2string(eneMeterTypeEntity.getMtModel()));
                    devCollectDeviceTypeEntity.setUpdateTime(new Date());
                    devCollectDeviceTypeEntity.setUpdateUserId(0);
                    devCollectDeviceTypeEntity.setIsValid(CommonOptions.YES_NO.YES);
                    devCollectDeviceTypeEntity.setState("1");

                    DevProdFactoryExample prodFactoryExample = new DevProdFactoryExample();
                    prodFactoryExample.createCriteria().andOutIdEqualTo(eneMeterTypeEntity.getPrId());
                    List<DevProdFactoryEntity> devProdFactoryEntities = devProdFactoryDao.selectByExample(prodFactoryExample);
                    if (devProdFactoryEntities.size() == 0) {
                        devCollectDeviceTypeEntity.setProdFactoryId(0);
                    } else {
                        DevProdFactoryEntity prodFactoryEntity = devProdFactoryEntities.get(0);
                        devCollectDeviceTypeEntity.setProdFactoryId(prodFactoryEntity.getId());

                    }

                    devCollectDeviceTypeDao.updateByPrimaryKey(devCollectDeviceTypeEntity);

                    updateCount++;
                } else {
                    DevCollectDeviceTypeEntity devCollectDeviceTypeEntity = new DevCollectDeviceTypeEntity();
                    devCollectDeviceTypeEntity.setOutId(eneMeterTypeEntity.getMtId());
                    devCollectDeviceTypeEntity.setDes(HzhrTypeUtil.object2string(eneMeterTypeEntity.getMtModel()));
                    devCollectDeviceTypeEntity.setCompanyId(Integer.parseInt(companyId));
                    devCollectDeviceTypeEntity.setCreateTime(new Date());
                    devCollectDeviceTypeEntity.setCreateUserId(0);
                    devCollectDeviceTypeEntity.setIsValid(CommonOptions.YES_NO.YES);
                    devCollectDeviceTypeEntity.setState("1");
                    devCollectDeviceTypeEntity.setDeviceCategoryCode(iotCategoryCode);

                    DevProdFactoryExample prodFactoryExample = new DevProdFactoryExample();
                    prodFactoryExample.createCriteria().andOutIdEqualTo(eneMeterTypeEntity.getPrId());
                    List<DevProdFactoryEntity> devProdFactoryEntities = devProdFactoryDao.selectByExample(prodFactoryExample);
                    if (devProdFactoryEntities.size() == 0) {
                        devCollectDeviceTypeEntity.setProdFactoryId(0);
                    } else {
                        DevProdFactoryEntity prodFactoryEntity = devProdFactoryEntities.get(0);
                        devCollectDeviceTypeEntity.setProdFactoryId(prodFactoryEntity.getId());

                    }

                    devCollectDeviceTypeDao.insert(devCollectDeviceTypeEntity);
                    addCount++;
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }

        //同步流量计型号
        String flowMeterCategoryCode = devOptionsService.getCode("COLLECT_DEVICE_CATEGORY", "FLOW_METER");
        eneMeterTypeExample = new EneMeterTypeExample();
        eneMeterTypeExample.createCriteria()
                .andRemoteMeterIdEqualTo("134")
                .andMeterCatagoryIdEqualTo("13");
        eneMeterTypeEntities = eneMeterTypeDao.selectByExample(eneMeterTypeExample);

        for (EneMeterTypeEntity eneMeterTypeEntity :
                eneMeterTypeEntities) {
            try {
                String mtId = eneMeterTypeEntity.getMtId();

                DevCollectDeviceTypeExample devCollectDeviceTypeExample = new DevCollectDeviceTypeExample();
                devCollectDeviceTypeExample.createCriteria()
                        .andOutIdEqualTo(mtId)
                        .andDeviceCategoryCodeEqualTo(flowMeterCategoryCode);
                List<DevCollectDeviceTypeEntity> devCollectDeviceTypeEntities = devCollectDeviceTypeDao.selectByExample(devCollectDeviceTypeExample);
                if (devCollectDeviceTypeEntities.size() > 0) {
                    DevCollectDeviceTypeEntity devCollectDeviceTypeEntity = devCollectDeviceTypeEntities.get(0);
                    devCollectDeviceTypeEntity.setDes(HzhrTypeUtil.object2string(eneMeterTypeEntity.getMtModel()));
                    devCollectDeviceTypeEntity.setUpdateTime(new Date());
                    devCollectDeviceTypeEntity.setUpdateUserId(0);
                    devCollectDeviceTypeEntity.setIsValid(CommonOptions.YES_NO.YES);
                    devCollectDeviceTypeEntity.setState("1");

                    DevProdFactoryExample prodFactoryExample = new DevProdFactoryExample();
                    prodFactoryExample.createCriteria().andOutIdEqualTo(eneMeterTypeEntity.getPrId());
                    List<DevProdFactoryEntity> devProdFactoryEntities = devProdFactoryDao.selectByExample(prodFactoryExample);
                    if (devProdFactoryEntities.size() == 0) {
                        devCollectDeviceTypeEntity.setProdFactoryId(0);
                    } else {
                        DevProdFactoryEntity prodFactoryEntity = devProdFactoryEntities.get(0);
                        devCollectDeviceTypeEntity.setProdFactoryId(prodFactoryEntity.getId());

                    }

                    devCollectDeviceTypeDao.updateByPrimaryKey(devCollectDeviceTypeEntity);
                    updateCount++;
                } else {
                    DevCollectDeviceTypeEntity devCollectDeviceTypeEntity = new DevCollectDeviceTypeEntity();
                    devCollectDeviceTypeEntity.setOutId(eneMeterTypeEntity.getMtId());
                    devCollectDeviceTypeEntity.setDes(HzhrTypeUtil.object2string(eneMeterTypeEntity.getMtModel()));
                    devCollectDeviceTypeEntity.setCompanyId(Integer.parseInt(companyId));
                    devCollectDeviceTypeEntity.setCreateTime(new Date());
                    devCollectDeviceTypeEntity.setCreateUserId(0);
                    devCollectDeviceTypeEntity.setIsValid(CommonOptions.YES_NO.YES);
                    devCollectDeviceTypeEntity.setState("1");
                    devCollectDeviceTypeEntity.setDeviceCategoryCode(flowMeterCategoryCode);

                    DevProdFactoryExample prodFactoryExample = new DevProdFactoryExample();
                    prodFactoryExample.createCriteria().andOutIdEqualTo(eneMeterTypeEntity.getPrId());
                    List<DevProdFactoryEntity> devProdFactoryEntities = devProdFactoryDao.selectByExample(prodFactoryExample);
                    if (devProdFactoryEntities.size() == 0) {
                        devCollectDeviceTypeEntity.setProdFactoryId(0);
                    } else {
                        DevProdFactoryEntity prodFactoryEntity = devProdFactoryEntities.get(0);
                        devCollectDeviceTypeEntity.setProdFactoryId(prodFactoryEntity.getId());

                    }

                    devCollectDeviceTypeDao.insert(devCollectDeviceTypeEntity);
                    addCount++;
                }
            } catch (Exception e) {
                e.printStackTrace();
            }

        }

        //同步LORA表型号
        String loraMeterCategoryCode = devOptionsService.getCode("COLLECT_DEVICE_CATEGORY", "LORA_METER");
        eneMeterTypeExample = new EneMeterTypeExample();
        eneMeterTypeExample.createCriteria()
                .andRemoteMeterIdEqualTo("132")
                .andMeterCatagoryIdEqualTo("13");
        eneMeterTypeEntities = eneMeterTypeDao.selectByExample(eneMeterTypeExample);

        for (EneMeterTypeEntity eneMeterTypeEntity :
                eneMeterTypeEntities) {
            try {
                String mtId = eneMeterTypeEntity.getMtId();

                DevCollectDeviceTypeExample devCollectDeviceTypeExample = new DevCollectDeviceTypeExample();
                devCollectDeviceTypeExample.createCriteria()
                        .andOutIdEqualTo(mtId)
                        .andDeviceCategoryCodeEqualTo(loraMeterCategoryCode);
                List<DevCollectDeviceTypeEntity> devCollectDeviceTypeEntities = devCollectDeviceTypeDao.selectByExample(devCollectDeviceTypeExample);
                if (devCollectDeviceTypeEntities.size() > 0) {
                    DevCollectDeviceTypeEntity devCollectDeviceTypeEntity = devCollectDeviceTypeEntities.get(0);
                    devCollectDeviceTypeEntity.setDes(HzhrTypeUtil.object2string(eneMeterTypeEntity.getMtModel()));
                    devCollectDeviceTypeEntity.setUpdateTime(new Date());
                    devCollectDeviceTypeEntity.setUpdateUserId(0);
                    devCollectDeviceTypeEntity.setIsValid(CommonOptions.YES_NO.YES);
                    devCollectDeviceTypeEntity.setState("1");

                    DevProdFactoryExample prodFactoryExample = new DevProdFactoryExample();
                    prodFactoryExample.createCriteria().andOutIdEqualTo(eneMeterTypeEntity.getPrId());
                    List<DevProdFactoryEntity> devProdFactoryEntities = devProdFactoryDao.selectByExample(prodFactoryExample);
                    if (devProdFactoryEntities.size() == 0) {
                        devCollectDeviceTypeEntity.setProdFactoryId(0);
                    } else {
                        DevProdFactoryEntity prodFactoryEntity = devProdFactoryEntities.get(0);
                        devCollectDeviceTypeEntity.setProdFactoryId(prodFactoryEntity.getId());

                    }

                    devCollectDeviceTypeDao.updateByPrimaryKey(devCollectDeviceTypeEntity);
                    updateCount++;
                } else {
                    DevCollectDeviceTypeEntity devCollectDeviceTypeEntity = new DevCollectDeviceTypeEntity();
                    devCollectDeviceTypeEntity.setOutId(eneMeterTypeEntity.getMtId());
                    devCollectDeviceTypeEntity.setDes(HzhrTypeUtil.object2string(eneMeterTypeEntity.getMtModel()));
                    devCollectDeviceTypeEntity.setCompanyId(Integer.parseInt(companyId));
                    devCollectDeviceTypeEntity.setCreateTime(new Date());
                    devCollectDeviceTypeEntity.setCreateUserId(0);
                    devCollectDeviceTypeEntity.setIsValid(CommonOptions.YES_NO.YES);
                    devCollectDeviceTypeEntity.setState("1");
                    devCollectDeviceTypeEntity.setDeviceCategoryCode(loraMeterCategoryCode);

                    DevProdFactoryExample prodFactoryExample = new DevProdFactoryExample();
                    prodFactoryExample.createCriteria().andOutIdEqualTo(eneMeterTypeEntity.getPrId());
                    List<DevProdFactoryEntity> devProdFactoryEntities = devProdFactoryDao.selectByExample(prodFactoryExample);
                    if (devProdFactoryEntities.size() == 0) {
                        devCollectDeviceTypeEntity.setProdFactoryId(0);
                    } else {
                        DevProdFactoryEntity prodFactoryEntity = devProdFactoryEntities.get(0);
                        devCollectDeviceTypeEntity.setProdFactoryId(prodFactoryEntity.getId());

                    }

                    devCollectDeviceTypeDao.insert(devCollectDeviceTypeEntity);
                    addCount++;
                }
            } catch (Exception e) {
                e.printStackTrace();
            }

        }

        //同步BK表型号
        String bkMeterCategoryCode = devOptionsService.getCode("COLLECT_DEVICE_CATEGORY", "BK_METER");

        eneMeterTypeEntities = eneMeterTypeCustomDao.selectBkMeterType();
        for (EneMeterTypeEntity eneMeterTypeEntity :
                eneMeterTypeEntities) {
            try {
                String mtId = eneMeterTypeEntity.getMtId();

                DevCollectDeviceTypeExample devCollectDeviceTypeExample = new DevCollectDeviceTypeExample();
                devCollectDeviceTypeExample.createCriteria()
                        .andOutIdEqualTo(mtId)
                        .andDeviceCategoryCodeEqualTo(bkMeterCategoryCode);
                List<DevCollectDeviceTypeEntity> devCollectDeviceTypeEntities = devCollectDeviceTypeDao.selectByExample(devCollectDeviceTypeExample);
                if (devCollectDeviceTypeEntities.size() > 0) {
                    DevCollectDeviceTypeEntity devCollectDeviceTypeEntity = devCollectDeviceTypeEntities.get(0);
                    devCollectDeviceTypeEntity.setDes(HzhrTypeUtil.object2string(eneMeterTypeEntity.getMtModel()));
                    devCollectDeviceTypeEntity.setUpdateTime(new Date());
                    devCollectDeviceTypeEntity.setUpdateUserId(0);
                    devCollectDeviceTypeEntity.setIsValid(CommonOptions.YES_NO.YES);
                    devCollectDeviceTypeEntity.setState("1");

                    DevProdFactoryExample prodFactoryExample = new DevProdFactoryExample();
                    prodFactoryExample.createCriteria().andOutIdEqualTo(eneMeterTypeEntity.getPrId());
                    List<DevProdFactoryEntity> devProdFactoryEntities = devProdFactoryDao.selectByExample(prodFactoryExample);
                    if (devProdFactoryEntities.size() == 0) {
                        devCollectDeviceTypeEntity.setProdFactoryId(0);
                    } else {
                        DevProdFactoryEntity prodFactoryEntity = devProdFactoryEntities.get(0);
                        devCollectDeviceTypeEntity.setProdFactoryId(prodFactoryEntity.getId());

                    }

                    devCollectDeviceTypeDao.updateByPrimaryKey(devCollectDeviceTypeEntity);
                    updateCount++;
                } else {
                    DevCollectDeviceTypeEntity devCollectDeviceTypeEntity = new DevCollectDeviceTypeEntity();
                    devCollectDeviceTypeEntity.setOutId(eneMeterTypeEntity.getMtId());
                    devCollectDeviceTypeEntity.setDes(HzhrTypeUtil.object2string(eneMeterTypeEntity.getMtModel()));
                    devCollectDeviceTypeEntity.setCompanyId(Integer.parseInt(companyId));
                    devCollectDeviceTypeEntity.setCreateTime(new Date());
                    devCollectDeviceTypeEntity.setCreateUserId(0);
                    devCollectDeviceTypeEntity.setIsValid(CommonOptions.YES_NO.YES);
                    devCollectDeviceTypeEntity.setState("1");
                    devCollectDeviceTypeEntity.setDeviceCategoryCode(bkMeterCategoryCode);

                    DevProdFactoryExample prodFactoryExample = new DevProdFactoryExample();
                    prodFactoryExample.createCriteria().andOutIdEqualTo(eneMeterTypeEntity.getPrId());
                    List<DevProdFactoryEntity> devProdFactoryEntities = devProdFactoryDao.selectByExample(prodFactoryExample);
                    if (devProdFactoryEntities.size() == 0) {
                        devCollectDeviceTypeEntity.setProdFactoryId(0);
                    } else {
                        DevProdFactoryEntity prodFactoryEntity = devProdFactoryEntities.get(0);
                        devCollectDeviceTypeEntity.setProdFactoryId(prodFactoryEntity.getId());
                    }

                    devCollectDeviceTypeDao.insert(devCollectDeviceTypeEntity);
                    addCount++;
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }

        long end = new Date().getTime();
        logger.info("同步采集设备型号结束: " + (end - start) / 1000 + "s");
        logger.info("同步时间: " + (end - start) / 1000 + "s");
        logger.info("更新数量: " + updateCount + "条");
        logger.info("新增数量: " + addCount + "条");

    }

}
